package com.drinks.yiniang.ui.fragment.user.password;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.utils.SharedPreUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.fragment.BaseFragment;
import com.hansen.library.ui.widget.button.SwitchButton;
import com.hansen.library.utils.StringUtils;

/**
 * 修改支付密码
 * A simple {@link Fragment} subclass.
 */
public class ModifyPayPasswordFragment extends BaseFragment implements SwitchButton.OnCheckedChangeListener {

    //switch button
    private SwitchButton switch_btn_modify_pay_pwd;
    //密码
    private EditText et_modify_pay_pwd;
    //确认密码
    private EditText et_modify_pay_pwd_repeat;
    //错误提示
    private TextView tv_modify_pay_pwd_error_tips;
    //确认
    private Button btn_modify_pay_pwd_sure;

    public ModifyPayPasswordFragment() {
        // Required empty public constructor
    }

    public static ModifyPayPasswordFragment newInstance() {
        return new ModifyPayPasswordFragment();
    }


    @Override
    protected int getResID() {
        return R.layout.fragment_modify_pay_password;
    }

    @Override
    protected void initView(View mView) {
        switch_btn_modify_pay_pwd = mView.findViewById(R.id.switch_btn_modify_pay_pwd);

        et_modify_pay_pwd = mView.findViewById(R.id.et_modify_pay_pwd);
        et_modify_pay_pwd_repeat = mView.findViewById(R.id.et_modify_pay_pwd_repeat);

        tv_modify_pay_pwd_error_tips = mView.findViewById(R.id.tv_modify_pay_pwd_error_tips);

        btn_modify_pay_pwd_sure = mView.findViewById(R.id.btn_modify_pay_pwd_sure);
    }

    @Override
    protected void initData() {
mContext = getActivity();

        setPaySettingData();
    }

    @Override
    protected void listener() {
        btn_modify_pay_pwd_sure.setOnClickListener(this);
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.btn_modify_pay_pwd_sure://确认
                setPayPassword();
                break;
                default:
                    break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }

    private void setPayPassword() {
        String pwd = StringUtils.getEditTextString(et_modify_pay_pwd.getText());

        if (StringUtils.isEmpty(pwd)) {
            //ToastUtils.showShort(R.string.text_hint_input_password);
            tv_modify_pay_pwd_error_tips.setText("*" + getString(R.string.text_hint_input_password));
            return;
        }

        if (pwd.length() != 6) {
            //ToastUtils.showShort(R.string.text_hint_input_six_pay_pwd);
            tv_modify_pay_pwd_error_tips.setText("*" + getString(R.string.text_hint_input_six_pay_pwd));
            return;
        }

        String repeatPwd = StringUtils.getEditTextString(et_modify_pay_pwd_repeat.getText());

        if (StringUtils.isEmpty(repeatPwd)) {
            //ToastUtils.showShort(R.string.text_hint_input_repeat_pwd);
            tv_modify_pay_pwd_error_tips.setText("*" + getString(R.string.text_hint_input_repeat_pwd));
            return;
        }

        if (!pwd.equals(repeatPwd)) {
            //ToastUtils.showShort(R.string.text_hint_two_input_pwd_not_consistent);
            tv_modify_pay_pwd_error_tips.setText("*" + getString(R.string.text_hint_two_input_pwd_not_consistent));
            return;
        }

        tv_modify_pay_pwd_error_tips.setText("");

        showLoadingDialog();

        final String isOpenPassword = switch_btn_modify_pay_pwd.isChecked() ? "1" : "0";

        UserApiIO.getInstance().setPayPassword(isOpenPassword, pwd, new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                ToastUtils.showShort(R.string.text_pwd_set_success);

                SharedPreUtils.getInstance().saveIsOpenPayPasswordID(isOpenPassword);

                if (getActivity() != null) {
                    getActivity().finish();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    private void setPaySettingData() {
        String isOpenPassword = SharedPreUtils.getInstance().getIsOpenPayPassword();

        switch_btn_modify_pay_pwd.setChecked(Constants.STATUS_GLOBAL_YES.equals(isOpenPassword) ? true : false);

        createSwitchListener();
    }

    private void createSwitchListener() {
        if(switch_btn_modify_pay_pwd != null) {
            switch_btn_modify_pay_pwd.setOnCheckedChangeListener(this);
        }
    }

    @Override
    public void onDestroy() {
        if(switch_btn_modify_pay_pwd != null) switch_btn_modify_pay_pwd.setOnCheckedChangeListener(null);
        super.onDestroy();
    }
}
